<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <!-- <a-form-item label="查找用户名">
                <a-input v-model="queryParam.username" placeholder="请输入用户名包含的字母"/>
              </a-form-item> -->
            </a-col>

            <a-col :md="!advanced && 8 || 24" :sm="24">
              <!-- <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="reset">重置</a-button>
              </span> -->
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
        <a-button type="primary" @click="handleJumpto">返回任务列表</a-button>
      </div>

      <s-table
        ref="table"
        size="default"
        :rowKey="(record,index)=>{return index}"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
              <a-popconfirm
                title="确定删除吗?"
                ok-text="确定"
                cancel-text="取消"
                @confirm="confirm(record)"
                @cancel="cancel(record)"
              >
              <a>删除</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>

      <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        :item="item"
        @getdata="getdata"
        @cancel="handleCancel"
        @ok="handleOk"
      />
        <updata-form
        ref="createModal"
        :visible="updata"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleUpdataCancel" 
        @ok="handleUpdataOk"
      />
      <step-by-step-modal ref="modal" @ok="handleOk"/>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { GetTaskData, AddRecode, DelRecode } from '../../../api/school'

import StepByStepModal from './modules/StepByStepModal'
import CreateForm from './modules/CreateForm'
import UpdataForm from './modules/UpdataForm'

const columns = []
export default {
  name: 'systemUsers',
  components: {
    STable,
    Ellipsis,
    CreateForm,
    StepByStepModal,
    UpdataForm
  },
  data () {
    return {
      // updata model
      updata: false,
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      columns,
      item: {},
      items: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam, this.items)
        console.log('loadData request parameters:', requestParameters)
        return GetTaskData(requestParameters)
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created () {
    let data = JSON.parse(this.$route.query.data)
    this.item = data
    this.items = {
      school_id: this.item.schoolId,
      task_id: this.item.id
    }
    console.log('--------------data--------------', data)
    console.log('-----------------item--------------', this.item)
    console.log('-----------------items--------------', this.items)
    // 操作
    const action = {
      title: '操作',
      dataIndex: 'action',
      width: '120px',
      scopedSlots: { customRender: 'action' }
    }
    // unshift
    const serial = {
      title: '编号',
      scopedSlots: { customRender: 'serial' }
    }

    let arr = data.forms.list.map(item => {
      return {title: item.label, dataIndex: item.model}
    })
    arr.unshift(serial)
    arr.push(action)
    this.columns = arr

    console.log('-------------coluns--------------', columns)
    // this.importData()
  },
  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    // 返回任务列表
    handleJumpto(){
      this.$router.push({
        name: 'tasklist',
        path: '/task/tasklist/index',
      })
    },
    // 删除
    confirm(e) {
      const form = this.$refs.createModal.form
      console.log(e);
      DelRecode({id: e.id}).then(res => {
        // 重置表单数据
        form.resetFields()
        // 刷新表格
        this.$refs.table.refresh()
        this.$message.success('删除成功');
      }).catch(error => {
        console.log('-------------error------------', error)
      })
    },
    cancel(e) {
      console.log(e);
      this.$message.error('已取消');
    },
    // 获取子组件数据
    getdata(values) {
      const form = this.$refs.createModal.form
      AddRecode({value: values, school_id: this.item.schoolId, task_id: this.item.id}).then(res => {
        this.visible = false
        this.confirmLoading = false
        // 重置表单数据
        form.resetFields()
        // 刷新表格
        this.$refs.table.refresh()

        this.$message.success('新增成功')
      })
    },
    handleAdd () {
      this.mdl = null
      this.visible = true
    },
    handleEdit (record) {
      this.visible = true
      this.mdl = { ...record }
    },
    // 修改密码
    handleUpdataCancel() {
      this.updata = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleUpdataOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((error, values) => {
        if(!error) {
          console.log('------------------values-------------', values)
          if (values.description === values.password) {
            const value = {id: values.id, password: values.password}
            UpdataUser(value).then(res => {
              this.updata = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('密码修改成功')
            })
          } else {
            this.$message.info('请确认密码保持一致')
            this.confirmLoading = false
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    // 创建
    handleOk () {
      // 触发子组件事件
      this.$refs.createModal.getData()
      const form = this.$refs.createModal.form
      this.confirmLoading = true
    },
    handleCancel () {
      this.visible = false

      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    // 显示修改密码窗口
    handleSub (record) {
      this.updata = true
      this.mdl = {...record}
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    },
    // 重置
    reset () {
      const form = this.$refs.createModal.form
      this.queryParam = {}
        // 重置表单数据
      form.resetFields()
      // 刷新表格
      this.$refs.table.refresh()
    },
  }
}
</script>
